<?php
function smarty_function_jpager($params, &$smarty)
{
	if (!isset($params['pages']) || $params['pages'] < 2) {
		return '';
	} else {
		$pages = $params['pages'];
	}
	
	$querys = array();
	parse_str($_SERVER['QUERY_STRING'], $querys);
	if (!isset($querys['page_no'])) {
		return '';
	}
	$current = $querys['page_no'];
	unset($querys['page_no']);
	
	if (isset($querys['ajax']) && $querys['ajax'] == true) {
		unset($querys['ajax']);
		$prefix = '#';
	} else {
		$prefix = '?';
	}
	$query = $prefix . http_build_query($querys);
	
	$left = false;
	$right = false;
	
	$html = '<div class="nav pagebar fix"><ul>';
	if ($current > 1) {
		$prev = $current - 1;
		$html .= "<li class=\"pagebarPrv\"><a href=\"{$query}&page_no={$prev}\">{$prev}</a></li>";
	}
	for ($i = 1; $i <= $pages; $i++) {
		if ($i <= 2 || $i > ($pages - 2) || (($current - 2) <= $i && $i <= ($current + 2))) {
			if ($i == $current) {
				$html .= "<li class=\"pagebarCurrent\">{$i}</li>";	
			} else {
				$html .= "<li><a href=\"{$query}&page_no={$i}\">{$i}</a></li>";
			}
		} else {
			if ($i < $current && !$left) {
				$html .= '<li class="pagebarDot">...</li>';
				$left = true;
			} elseif ($i > $current && !$right) {
				$html .= '<li class="pagebarDot">...</li>';
				$right = true;
			}
		}
	}
	if ($current < $pages) {
		$next = $current + 1;
		$html .= "<li class=\"pagebarNext\"><a href=\"{$query}&page_no={$next}\">{$next}</a></li>";
	}
	$html .= '</ul></div>';
	
	return $html;
}
?>